package com.hson.demo.base.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.hson.demo.base.dto.PageInfo;

/**
 * 通用分页类
 * 
 * @param <T>
 */
public class Pagination<T> implements Serializable {
	private static final long serialVersionUID = 2402039579750346552L;
	
	/* 总记录数 */
	private int recordTotal = 0;
	
	/* 当前是第几页 */
	private int currentPage = 1;
	
	/* 每页显示多少条记录 */
	private int pageSize = 10;
	
	/* 每页显示多少个分页 */
	private int pageStep = 10;
	
	/* 总页数 */
	private int pageTotal = 0;
	
	/* 首页 */
	private int firstPage = 0;
	
	/* 尾页 */
	private int lastPage = 0;
	
	/* 上一页 */
	private int prevPage = 0;
	
	/* 下一页 */
	private int nextPage = 0;
	
	/* 当前页码的数组 */
	private int[] pageList = {};
	
	/* 记录起始位置 */
	private int recordStart = 0;
	
	/* 记录结束位置 */
	private int recordEnd = 0;
	
	/* 当前分页的记录 */
	private List<T> records = new ArrayList<T>();
	
	/**
	 * 构造函数
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param currentPage 当前是第几页
	 */
	public Pagination(int currentPage) {
		if (currentPage > 1) {
			this.currentPage = currentPage;
		}
	}
	
	/**
	 * 构造函数
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param currentPage 当前是第几页
	 * @param pageSize 每页显示多少条记录
	 */
	public Pagination(int currentPage, int pageSize) {
		if (currentPage > 1) {
			this.currentPage = currentPage;
		}
		
		if (pageSize > 1) {
			this.pageSize = pageSize;
		}
	}
	
	/**
	 * 构造函数
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param currentPage 当前是第几页
	 * @param pageSize 每页显示多少条记录
	 * @param pageStep 每页显示多少个分页
	 */
	public Pagination(int currentPage, int pageSize, int pageStep) {
		if (currentPage > 1) {
			this.currentPage = currentPage;
		}
		
		if (pageSize > 1) {
			this.pageSize = pageSize;
		}
		
		if (pageStep > 1) {
			this.pageStep = pageStep;
		}
	}
	
	/**
	 * 生成分页信息
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 */
	public void generate() {
		if (recordTotal < 1) {
			return;
		}
		
		double d1 = Math.ceil(Double.parseDouble(Integer.toString(recordTotal)) / Double.parseDouble(Integer.toString(pageSize)));
		
		pageTotal = Integer.parseInt(Double.toString(d1).replaceAll("\\.[0-9]*$", ""));
		
		if (currentPage < 1) {
			currentPage = 1;
		}
		
		if (currentPage > pageTotal) {
			currentPage = pageTotal;
		}
		
		firstPage = 1;
		
		lastPage = pageTotal;
		
		prevPage = currentPage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		
		nextPage = currentPage + 1;
		if (nextPage > pageTotal) {
			nextPage = pageTotal;
		}
		
		double d2 = Math.ceil(Double.parseDouble(Integer.toString(currentPage)) / Double.parseDouble(Integer.toString(pageStep)));
		
		int k = Integer.parseInt(Double.toString(d2).replaceAll("\\.[0-9]*$", ""));
		
		int n = (k - 1) * pageStep + 1;
		
		int m = k * pageStep;
		if (m > pageTotal) {
			m = pageTotal;
		}
		
		List<Integer> pageList = new ArrayList<Integer>();
		
		for (int i = n; i <= m; i++) {
			pageList.add(Integer.valueOf(i));
		}
		
		this.pageList = toIntArray(pageList);
		
		recordStart = (currentPage - 1) * pageSize + 1;
		
		recordEnd = currentPage * pageSize;
		if (recordEnd > recordTotal) {
			recordEnd = recordTotal;
		}
	}

	public int getRecordTotal() {
		return recordTotal;
	}

	public void setRecordTotal(int recordTotal) {
		this.recordTotal = recordTotal;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageStep() {
		return pageStep;
	}

	public void setPageStep(int pageStep) {
		this.pageStep = pageStep;
	}

	public int getPageTotal() {
		return pageTotal;
	}

	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int[] getPageList() {
		return pageList;
	}

	public void setPageList(int[] pageList) {
		this.pageList = pageList;
	}

	public int getRecordStart() {
		return recordStart;
	}

	public void setRecordStart(int recordStart) {
		this.recordStart = recordStart;
	}

	public int getRecordEnd() {
		return recordEnd;
	}

	public void setRecordEnd(int recordEnd) {
		this.recordEnd = recordEnd;
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}
	
	private int[] toIntArray(List<Integer> list) {
		if (list == null || list.isEmpty()) {
			return new int[] {};
		}
		
		int[] ret = new int[list.size()];
		
		int j = 0;
		
		for (Integer i : list) {
			ret[j] = i.intValue();
			
			j++;
		}
		
		return ret;
	}
	
	public PageInfo getPageInfo(){
		PageInfo page = new PageInfo();
		page.setPage(getCurrentPage());
		page.setTotal(getRecordTotal());
		page.setPageSize(getPageSize());
		page.setMaxPage(getPageTotal());
		return page;
	}
}